package com.example.back.Repository;

import com.example.back.NonRepEntity.NickNameAndSoldNum;
import com.example.back.NonRepEntity.SoldNumAndLevel;
import com.example.back.Entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    User findUserByUserId(long userId);//通过ID查询用户

    @Override
    List<User> findAll();//查询全部用户

    User findUserByUserName(String userName);//根据用户名查询用户

    @Query(value = "select new com.example.back.NonRepEntity.NickNameAndSoldNum(u.userId,u.nickName,u.soldNum) from User u")
    List<NickNameAndSoldNum> findTopSeller(Pageable pageable);

    @Query(value = "select new com.example.back.NonRepEntity.SoldNumAndLevel(u.logisticsLevel,u.productLevel,u.serviceLevel,u.soldNum) from User u where u.userId=:userId")
    SoldNumAndLevel getNumAndLevelByUserId(long userId);

    @Transactional
    @Modifying
    @Query(value = "update User u set u.soldNum=:newNum,u.serviceLevel=:newSLevel,u.logisticsLevel=:newLLevel,u.productLevel=:newPLevel where u.userId=:userId")
    void updateSellerInfo(int newNum, double newSLevel, double newLLevel, double newPLevel, long userId);
}
